package sample.dao.article.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sample.intf.article.dao.IArticlePictureDao;
import sample.intf.article.dto.TArticlePicture;

@Repository
public class ArticlePictureDao implements IArticlePictureDao {

	private static final String NAMESPACE = "sample.articlepicture.";

	@Autowired
	SqlSessionTemplate sqlSessionTemplate;

	@Override
	public TArticlePicture selectArticlePicture(String guid) {
		return sqlSessionTemplate.selectOne(NAMESPACE + "selectArticlePictureByGuid", guid);
	}

	@Override
	public List<TArticlePicture> selectArticlePictures(long articleId) {
		return sqlSessionTemplate.selectList(NAMESPACE + "selectArticlePicturesByArticleId", articleId);
	}

	@Override
	public List<TArticlePicture> selectMissingArticlePictures(long articleId, List<String> guids) {
		if (guids.size() > 0) {
			Map<String, Object> params = new HashMap<>();
			params.put("articleId", articleId);
			params.put("guids", guids);
			return sqlSessionTemplate.selectList("selectMissingArticlePicturesByGuid", params);
		}
		return sqlSessionTemplate.selectList(NAMESPACE + "selectArticlePicturesByArticleId", articleId);
	}

	@Override
	public void insertArticlePicture(TArticlePicture articlePicture) {
		sqlSessionTemplate.insert(NAMESPACE + "insertArticlePicture", articlePicture);
	}

	@Override
	public void deleteArticlePicture(String guid) {
		sqlSessionTemplate.delete(NAMESPACE + "deleteArticlePictureByGuid", guid);
	}

	@Override
	public void deleteArticlePictures(long articleId) {
		sqlSessionTemplate.delete(NAMESPACE + "deleteArticlePicturesByArticleId", articleId);
	}

	@Override
	public void deleteArticlePictures(List<String> guids) {
		if (guids.size() > 0)
			sqlSessionTemplate.delete(NAMESPACE + "deleteArticlePicturesByGuids", guids);
	}
}
